﻿Public Class frmXLSProductUp




#Region "프로시져"

    '## 중복체크 
    Private Sub productDupCheck()
        Call dbConnection()
        Dim corrCnt As Integer = 0
        Dim inCorrCnt As Integer = 0

        If radio1.Checked = True Then
            For i = 0 To grdTmpXlsProduct.Rows.Count - 1

                Dim tmpSql = "SELECT count(*) FROM tblProduct WHERE prdNm='" + grdTmpXlsProduct.Rows(i).Cells(1).Value + "' AND part='" + grdTmpXlsProduct.Rows(i).Cells(3).Value + "' AND size='" + grdTmpXlsProduct.Rows(i).Cells(5).Value + "' AND  flag<>'N'"
                Dim isProduct As String = ""
                MsgBox(tmpSql)
                isProduct = gAFieldValue(tmpSql)
                If isProduct = "0" Then
                    grdTmpXlsProduct.Rows(i).Cells(0).Value = True
                    corrCnt += 1
                Else
                    grdTmpXlsProduct.Rows(i).DefaultCellStyle.BackColor = Color.DarkOrange
                    inCorrCnt += 1
                End If
            Next
        Else
        End If
        linkLb.Visible = False
        lbDupRslt.Text = "유효 : " + corrCnt.ToString + " / 중복 : " + inCorrCnt.ToString + " / 총 : " + grdTmpXlsProduct.Rows.Count.ToString

        dbConn.Close()
    End Sub

    '## 데이터 적용
    Private Sub appendCompany()
        Dim appendCompCnt As Integer = 0
        Dim company, compNo, owner, phone, fax, major, cellPhone, addr, types, article As String
        Dim curDt As DateTime = Now
        Dim alertMsg As String = ""

        If grdTmpXlsProduct.Rows.Count = 0 Then
            If radio1.Checked = True Then
                alertMsg = "제품 등록에 적용할 데이터가 없습니다."
            Else
                alertMsg = "바크드 등록에 적용할 데이터가 없습니다."
            End If
            MsgBox(alertMsg)
            Exit Sub
        End If

        If radio1.Checked = True Then
            If grdTmpXlsProduct.Rows(0).Cells.Count <> 11 Then
                alertMsg = "엑셀 업로드를 위한 제품 양식이 정상적이지 않습니다."
            End If
        Else
            If grdTmpXlsProduct.Rows(0).Cells.Count <> 6 Then
                alertMsg = "엑셀 업로드를 위한 바코드 양식이 정상적이지 않습니다."
            End If
        End If
        MsgBox(alertMsg)
        Exit Sub

        Call dbConnection()

        For i = 0 To grdTmpXlsProduct.Rows.Count - 1
            company = ""
            compNo = ""
            owner = ""
            phone = ""
            fax = ""
            major = ""
            cellPhone = ""
            addr = ""
            types = ""
            article = ""
            If grdTmpXlsProduct.Rows(i).Cells(0).Value.ToString = "True" Then
                company = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(1).Value.ToString)
                compNo = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(2).Value.ToString)
                owner = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(3).Value.ToString)
                phone = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(4).Value.ToString)
                fax = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(5).Value.ToString)
                major = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(6).Value.ToString)
                cellPhone = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(7).Value.ToString)
                addr = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(8).Value.ToString)
                types = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(9).Value.ToString)
                article = gFnChkVal(grdTmpXlsProduct.Rows(i).Cells(10).Value.ToString)
                Dim compSql As String
                compSql = "INSERT INTO tblCompany(company, compNo, owner, phone, fax, major, cellPhone, addr, types, article, addDt) " + _
                             " VALUES('" + company + "', '" + compNo + "', '" + owner + "', '" + phone + "', '" + fax + "', '" + major + "', '" + cellPhone + "', '" + addr + "', '" + types + "', '" + article + "','" + curDt + "')"
                Call gSubSaveAction(compSql, "")
                appendCompCnt += 1
            End If
        Next

        dbConn.Close()
        If appendCompCnt = 0 Then
            MsgBox("중복체크를 실행해 주시기 바랍니다.")
            btnCheckData.Focus()
            Exit Sub
        End If
        MsgBox("거래처 정보가 등록되었습니다")
    End Sub
#End Region

#Region "이벤트"

    Private Sub frmXLSProductUp_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        btnXlsOpen.Text = "제품 엑셀 열기"
    End Sub

    Private Sub btnXlsOpen_Click(sender As System.Object, e As System.EventArgs) Handles btnXlsOpen.Click
        Call gSubImportXLS(grdTmpXlsProduct)
        lbDupRslt.Text = ""
    End Sub

    Private Sub radio1_Click(sender As Object, e As System.EventArgs) Handles radio1.Click
        btnXlsOpen.Text = "제품 엑셀 열기"
    End Sub

    Private Sub radio2_Click(sender As Object, e As System.EventArgs) Handles radio2.Click
        btnXlsOpen.Text = "바코드 엑셀 열기"
    End Sub

    Private Sub btnCheckData_Click(sender As System.Object, e As System.EventArgs) Handles btnCheckData.Click
        Call productDupCheck()
    End Sub

#End Region

End Class